import { Cmd2dDrawText, Cmd2dSetFillColor, CMD_2D_CLEAR } from "../../dev/KaseoCanvasGraphicsDI";
import KaseoProgram from "../../KaseoProgram";
import { KaseoRuntimeBridge } from "../../KaseoRuntime";

export default class extends KaseoProgram {
    constructor() {
        super('KASEO-BOOT')
    }

    onLaunch(bridge: KaseoRuntimeBridge): void {
        const exGdi = bridge.getExtraGraphicsGDI()
        const mjGdi = bridge.getMajorGraphicsGDI()
        const kdi = bridge.getMajorKeyboardDI()

        if (exGdi == null)
            return

        const gdis = [exGdi, mjGdi]
        const texts = ['Welcome', 'KASEO']
        const sizes = [45, 80]
    
        gdis.forEach((gdi, idx) => {
            if (!gdi)
                return

            gdi.setContextFontSize(sizes[idx])
            const mat = gdi.measureText(texts[idx])
            const [w, h] = [gdi.getScreenWidth(), gdi.getScreenHeight()]
            const [tw, th] = [mat.width, mat.actualBoundingBoxAscent]

            gdi.pushCommand2d(CMD_2D_CLEAR)
            gdi.pushCommand2d(new Cmd2dSetFillColor(1, 1, kdi?.getKeyState(35) ? 0 : 1))
            gdi.pushCommand2d(new Cmd2dDrawText(texts[idx], w / 2 - tw / 2, h / 2 + th / 2))
        })
    }

    onKnock(bridge: KaseoRuntimeBridge): void {
        this.onLaunch(bridge)
    }
}
